package com.mhkj.base.rbac.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mhkj.base.rbac.role.service.MyRoleService;
import com.mhkj.base.rbac.user.dao.SystemUserRepository;
import com.mhkj.base.rbac.user.entity.SystemUser;
import com.mhkj.base.rbac.user.service.SystemUserService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.SimpleCode;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 系统用户
 * @author mics
 * @date 2018年9月1日
 * @version 1.0
 */
@Service
public class SystemUserServiceImpl extends BaseServiceImpl<SystemUser> implements SystemUserService{
	@Resource
	SystemUserRepository systemUserRepository;
	@Resource
	private OrgService orgService;
	@Resource
	private SimpleCode simpleCode;
	@Resource
	private MyRoleService myRoleService;
	
	@Resource
	public void setSystemUserRepository(SystemUserRepository systemUserRepository) {
		super.setBaseRepository(systemUserRepository);
	}

	@Override
	public Pagination<SystemUser> listByPage(SystemUser systemUser,
			int pageIndex, int pageSize) {
		Specification<SystemUser> spec = new Specification<SystemUser>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<SystemUser> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				if(StringUtils.hasText(systemUser.getName())){
	                   Predicate p1=criteriaBuilder.like(root.get("name").as(String.class), "%"+systemUser.getName()+"%");
	                   lstPredicates.add(p1);
				}
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<SystemUser> pageT = systemUserRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
		Pagination<SystemUser> pagination = new Pagination<SystemUser>();
		pagination.setTotal(systemUserRepository.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	@Transactional
	public void save(SystemRole systemRole, String userIds) {
		String[] uids = userIds.split(",");
		for(String uid:uids){
			SystemUser systemUser = systemUserRepository.findByOaId(uid);
			if(systemUser == null){
				Employee employee = orgService.employeeInfo(uid);
				systemUser = new SystemUser();
				systemUser.setName(employee.getName());
				systemUser.setLoginName(employee.getLoginName());
				String code = simpleCode.generatorSimpleCode(SystemUser.class, "code");
				systemUser.setCode(code);
				systemUser.setOaId(employee.getId());
				List<SystemRole> systemRoles = new ArrayList<SystemRole>();
				systemRoles.add(systemRole);
				systemUser.setSystemRoles(systemRoles);
				save(systemUser);
			}else{
				List<SystemRole> systemRoles = systemUser.getSystemRoles();
				systemRoles.add(systemRole);
				edit(systemUser);
			}
		
		}		
	}

	@Override
	@Transactional
	public void edit(SystemUser systemUser, String roleIds) {
		SystemUser systemUsered = getById(systemUser.getId());
		systemUser.setCode(systemUsered.getCode());
		systemUser.setLoginName(systemUsered.getLoginName());
		systemUser.setOaId(systemUsered.getOaId());
		String[] roleIdsTemp = roleIds.split(",");
		List<SystemRole> systemRoles = new ArrayList<SystemRole>();
		for(String rid:roleIdsTemp){
			SystemRole systemRole = myRoleService.getById(Long.valueOf(rid));
			systemRoles.add(systemRole);
			systemUser.setSystemRoles(systemRoles);
		}
		edit(systemUser);		
	}

	@Override
	public SystemUser getByOaId(String oaId) {
		SystemUser systemUser = systemUserRepository.findByOaId(oaId);
		return systemUser;
	}

	@Override
	public List<Object[]> getUserRole(String loginName) {
		return systemUserRepository.getUserRole(loginName);
	}

	@Override
	public SystemUser getByName(String name) {
		return systemUserRepository.getByName(name);
	}

	@Override
	public SystemUser getByLoginName(String loginName) {
		return systemUserRepository.getByLoginName(loginName);
	}
	
}
